import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fulong.MainApplication;

import com.fulong.entity.DeviceManageInfoEntity;

import com.fulong.service.DeviceManageInfoService;
import com.fulong.service.Job.A;
import com.fulong.service.Job.DeviceSqlJob;
import com.fulong.service.UserMsgInfoService;

import org.junit.jupiter.api.Test;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import javax.sound.midi.Soundbank;
import java.io.File;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

//@SpringBootTest(classes = MainApplication.class)
public class Mytest {

    @Autowired
    UserMsgInfoService userMsgInfoService;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    DeviceManageInfoService deviceManageInfoService;

    @Autowired
    Scheduler scheduler;

    @Test
    void testsql(){

//boolean update = deviceManageInfoService.update(new UpdateWrapper<DeviceManageInfoEntity>().setSql("UPDATE device_manage_info set device_manage_info.period =  device_manage_info.period-1"));

        System.out.println(deviceManageInfoService.update(new UpdateWrapper<DeviceManageInfoEntity>().setSql("device_manage_info.period =  device_manage_info.period-1")));
    }

    @Test
    public void test_string(){
        String voidSource = "{\n" +
                "%s\n" +
                "   try{\n" +
                "       %s$agent($$);\n" +
                "   } cathc(Throwable e) {\n" +
                "     %s\n" +
                "       throw e ;\n" +
                "   }finally{\n" +
                "     %s" +
                "   }\n" +
                "}\n";
        String beginSrc = "Object stat = Servlet_Agent.HttpCollect.begin($args);";
        String errorSrc = "Servlet_Agent.HttpCollect.error(e, stat);";
        String endSrc = "Servlet_Agent.HttpCollect.end(stat);";
        System.out.println(String.format(voidSource,beginSrc,"service",errorSrc,endSrc));
    }

    @Test
    public void smyboltest() {
//        String s = "select pwd from user_baise_info where id=?" ;
//        System.out.println(s.replace("?" , "132"));
//        System.out.println(s);
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
    }
    @Test
    public void dateTest() {
//        Map<String, Object> map = jdbcTemplate.queryForMap("select * from user_base_info");
//        userBaiseInfoEntities.forEach(System.out::println);
        String s1 = "s1" ;
        String s2 = "s1" ;
        System.out.println( s1 == s2);
    }

    @Test
    public void maptest(){
        HashMap<Integer, String> params = new HashMap<>();
        params.put(1, UUID.randomUUID().toString());
        params.put(2,"2");
        params.put(3,"3");
        params.put(4,"4");
        params.put(5,"5");
        String sql = "insert into device (a,b,c,d,e) values(?,?,?,?,?)";
        for (int i = 1  ; i <= params.size() ; i++) {
            sql = sql.replaceFirst("\\?" , params.get(i));
        }

        System.out.println(sql);

    }

    @Test
    public void arrayList(){
        File f = new File("E:\\JavaWeb_Test\\simulation\\video\\test.mp4") ;
        System.out.println(f.getAbsolutePath());
    }

    @Test
    public  void  forDebug(){
        int count = 0 ;
        for(int i = 0 ; i < 1000*1000*10 ; i++) {
            count++ ;
        }
    }

    @Test
    public void myListTest(){
        List<Integer> list = new LinkedList<>();
        for (int i = 0 ; i< 10 ; i ++) {
            list.add(i+1) ;
        }
        for (int i = 0 ; i< 10 ; i ++) {
            System.out.println(list.get(i));
        }
    }
    @Test
    public void geerate(){
        int numRows = 5 ;
        List<List<Integer>> result = new LinkedList<>();
        for(int i = 0 ; i < numRows ; i ++) {
            List<Integer> list = new LinkedList<>();
            list.add(1) ;
            for(int j = 1 ; j < i  ; j++) {
                list.add(result.get(i-1).get(j)+result.get(i-1).get(j-1));
            }
            if(i!=0) {
                list.add(1);
            }

            result.add(list) ;
        }
        for(List<Integer> list : result) {
            for(int i = 0 ; i < list.size() ; i++) {
                System.out.print(list.get(i)+"\t");
            }
            System.out.println();
        }
    }



    @Test
    void  testschudeler() throws SchedulerException, InterruptedException {
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("sql","sql1");
        JobDataMap jobDataMap1 = new JobDataMap();
        jobDataMap.put("sql","sql2");
        JobDetail job = JobBuilder.newJob(DeviceSqlJob.class)
                .withIdentity("kk")
                .build();
        JobDetail job1 = JobBuilder.newJob(DeviceSqlJob.class)
                .withIdentity("kkk")
                .setJobData(jobDataMap1)
                .setJobData(jobDataMap)
                .build();
        //2.创建trigger
        CronTrigger trigger = TriggerBuilder.newTrigger()
                .withSchedule(CronScheduleBuilder.cronSchedule("0/3 * * * * ?"))
                .withIdentity("job1")
                .build();
        CronTrigger trigger1 = TriggerBuilder.newTrigger()
                .withSchedule(CronScheduleBuilder.cronSchedule("0/3 * * * * ?"))
                .withIdentity("job2")
                .build();
        //3.创建schedule
//        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
//        scheduler.start();

        scheduler.scheduleJob(job, trigger);
        scheduler.scheduleJob(job1, trigger1);
//        int count = 1 << 16 ;
//        new Thread(()->{
//            try {
//                TimeUnit.SECONDS.sleep(10);
//                scheduler.deleteJob(JobKey.jobKey("kk"));
//                TimeUnit.SECONDS.sleep(10);
//
//            } catch (InterruptedException | SchedulerException e) {
//                e.printStackTrace();
//            }
//        }).start();
        TimeUnit.SECONDS.sleep(100);

    }

    @Test
    public void quicksort(){
        int arr[] = {21,3,23,3,4,5,6,4,56,7,5,67,};
        int left = 0 ;
        int right = arr.length-1 ;
        while( left < right) {
            while (left < right && arr[left] < arr[right]) {
                left++ ;
            }
            arr[left] ^= arr[right] ;
            arr[right] ^=arr[left];
            arr[left] ^= arr[right];
            while (left < right && arr[left] < arr[right]) {
                right -- ;
            }
            arr[left] ^= arr[right] ;
            arr[right] ^=arr[left];
            arr[left] ^= arr[right];
        }
        for(int el : arr) {
            System.out.println(el);
        }
    }

    @Test
    public void cro(){

    }
}
